<?php 
class MemderController{
	private $data;
//	数据库
	public function __construct(){
		$this->data = include './data.php';
	}
//	注册
	public function reg(){
		if(IS_POST){
			foreach ($this->data as $u) {
				if($u['username'] == $_POST['username']){
					success('该用户名已存在', 'index.php?c=Memder&a=reg');
				}
			}
			$this->data[] = array(
			'username' => $_POST['username'],
			'password' => md5($_POST['password'])
			);
			file_put_contents('./data.php', "<?php return " .var_export($this->data,true). "?>");
			success('注册成功','index.php');
		}
		include './View/reg.php';
	}
//	登录
	public function login(){
		if(IS_POST){
			$username = $_POST['username'];
			$password = md5($_POST['password']);
			if(strtolower($_POST['yanzheng']) != $_SESSION['thts']){
				success('验证失败', 'index.php?c=Memder&a=login');
			}
//			p($_POST);
			foreach ($this->data as $u) {
				if($u['username'] ==$username && $u['password'] ==  $password){
					$_SESSION['username'] = $username;
					if($_POST=='auto'){
						setcookie(session_name(),session_id(),time() + 3600 * 24 * 7,'/');
					}else{
						setcookie(session_name(),session_id(),0,'/');
					}
					success('登录成功', 'index.php');
				}		
			}
			success('登陆失败','index.php?c=Memder&a=login');
		}
		 include './View/login.php';
	}
//	退出
	public function out(){
		session_unset();
		session_destroy();
		success('退出成功', 'index.php');
	}
//	用户名异步
	public function checkuser(){
		$username = $_POST['u'];
		foreach ($this->data as $u) {
			if($u['username'] == $username){
				echo 1;
				exit;
			}
			if($username == null){
				echo 3;
				exit;
			}
		}
		echo 0; 
	}
//	验证码异步
	public function vcofeuser(){
		$username =strtolower($_POST['u']);
		if($_SESSION['thts'] == $username){
			echo 1;
			exit;
		}else{
			echo 0;
		}
	}
}











?>